﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WXY.Course.BLL;
using WXY.Course.Common.MVCExtension;
using WXY.Course.Model;
using WXY.Course.Web.Areas.Admin.Filter;
using WXY.Course.Web.Areas.Admin.Models;

namespace WXY.Course.Web.Areas.Admin.Controllers
{
    [DefaultAuthorize]
    public class ResourceController : Controller
    {
        ResourceBLL _resourceBLL = new ResourceBLL();
        ModuleBLL _moduleBLL = new ModuleBLL();

        public ActionResult List(int? page, int? categoryId, string title, DateTime? beginTime, DateTime? endTime)
        {
            if (!page.HasValue) page = 1;
            if (!categoryId.HasValue) categoryId = -1;
            if (endTime.HasValue) endTime = endTime.Value.AddDays(1);

            PageParams pageParams = new PageParams
            {
                PageIndex = page.Value,
                PageSize = 10,
                BeginTime = beginTime,
                EndTime = endTime,
                IsNeedRecordCount = true
            };

            PageViewModel<ResourceInfo> pageViewModel = new PageViewModel<ResourceInfo>();

            pageViewModel.Value = _resourceBLL.GetPageList(categoryId.Value, title, pageParams);
            pageViewModel.PageInfo = PageInfo.Create(pageParams.PageIndex, pageParams.PageSize);
            pageViewModel.PageInfo.RecordCount = pageParams.RecordCount;

            ViewData["ModuleList"] = _moduleBLL.GetList();
            return View(pageViewModel);
        }

        public ViewResult Edit(int? id)
        {
            ViewData["ModuleList"] = _moduleBLL.GetList();
            ResourceInfo info = new ResourceInfo();
            if (id.HasValue)
            {
                info = _resourceBLL.Get(id.Value);
            }
            return View(info);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(int? id, int? moduleId, string title, string content)
        {
            UserInfo userInfo = HttpContext.Items["UserInfo"] as UserInfo;

            if (!id.HasValue) id = -1;
            if (!moduleId.HasValue) moduleId = 0;

            var info = new ResourceInfo
            {
                Code = id.Value,
                ModuleInfo = new ModuleInfo
                {
                    Code = moduleId.Value
                },
                Title = title,
                Content = content
            };

            bool result = false;
            if (info.Code > 0)
            {
                info.UpdateTime = DateTime.Now;
                result = _resourceBLL.Update(info) > 0;
            }
            else
            {
                info.CreateTime = DateTime.Now;
                info.UpdateTime = DateTime.Now;
                info.UserInfo = userInfo;

                result = _resourceBLL.Insert(info) > 0;
            }
            return Json(new { IsSuccess = result });
        }

        [HttpPost]
        [JsonExceptionFilter(typeof(ResourceController), JsonRequestBehavior.AllowGet)]
        public JsonResult Delete(int id)
        {
            var result = new BoolResult<int>
            {
                IsSuccess = true,
                Data = _resourceBLL.Delete(id)
            };
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        [JsonExceptionFilter(typeof(ResourceController), JsonRequestBehavior.AllowGet)]
        public JsonResult Recommend(int id)
        {
            var result = new BoolResult<int>
            {
                IsSuccess = true,
                Data = _resourceBLL.Recommend(id)
            };
            return Json(result, JsonRequestBehavior.AllowGet);
        }
    }
}
